<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>axios的基本使用</title>
    <!-- 1. 引入 axios 文件 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
    <button>发送 GET 请求</button>
    <button>发送 POST 请求</button>
    <button>发送 DELETE 请求</button>
    <button>发送 PUT 请求</button>
    <button>发送 Patch 请求</button>

    <script>
        //2. 绑定事件 发送请求
        let btns = document.querySelectorAll('button');

        //GET 请求
        btns[0].onclick = function(){
            //调用  axios 函数
            axios({
                //请求类型
                method: 'get',
                //请求 url
                url: 'http://127.0.0.1/server',
                //查询字符串
                params: {
                    a: 100,
                    b: 200
                },
                //设置请求头
                headers: {
                    c: 300,
                    d: 400
                }
            }).then(response => {
                console.log(response);
            }, error=>{
                console.log(error);
            })
        }
        
        //POST 请求  重要 重要  重要
        btns[1].onclick = async function(){
            let result = await axios({
                //请求行
                method: 'post',
                url: 'http://127.0.0.1/server',
                params: {
                    a:100,
                    b:200
                },
                //请求头
                headers:{
                    c: 300,
                    d: 400
                },
                //请求体
                // data: {
                //     e: 500,
                //     f: 600
                // }
                data: 'e=500&f=600'
            })
        
            // console.log(result);
            console.log(result.status);
            console.log(result.headers);
            console.log(result.data);
        }
        
        //DELETE 请求
        btns[2].onclick = async function(){
            let result = await axios({
                method: 'delete',
                url: 'http://127.0.0.1/server'
            });
            console.log(result);
        }
        
        //Put 请求
        btns[3].onclick = async function(){
            let result = await axios({
                method : 'put',
                url: 'http://127.0.0.1/server'
            })
            console.log(result);
        }

        //patch
        btns[4].onclick = async function(){
            let result = await axios({
                method : 'patch',
                url: 'http://127.0.0.1/server'
            })
            console.log(result);
        }
    </script>
</body>
</html>